Find the live weather report of a given city¶
Find the live weather report (temperature, wind speed, description and weather) of a given city.
import requests
from pprint import pprint
def weather_data(query):
res=requests.get('http://api.openweathermap.org/data/2.5/weather?' +
query +
'&APPID=b35975e18dc93725acb092f7272cc6b8&units=metric');
return res.json();
def print_weather(result, city):
print("{}'s temperature: {}°C ".format(city,result['main']['temp']))
print("Wind speed: {} m/s".format(result['wind']['speed']))
print("Description: {}".format(result['weather'][0]['description']))
print("Weather: {}".format(result['weather'][0]['main']))
def main():
city=input('Enter the city:')
print()
try:
query ='q = '+city;
w_data = weather_data(query);
print_weather(w_data, city)
print()
except:
print('City name not found...')
if __name__=='__main__':
main()
Output:
Enter the city: Brazil
Brazil's temperature: 16.45°C
Wind speed: 2.1 m/s
Description: clear sky
Weather: Clear